//
// Ishtar C++ Utility Library
//
// I/O Amenity - I/O Data Header
//

#ifndef __ISHTAR_IO_IO_DATA_H
#define __ISHTAR_IO_IO_DATA_H

#include <Ishtar/Ishtar.h>

#if defined( ISHTAR_COMPILER_HAS_PRAGMA_ONCE )
#pragma once
#endif

#include <Ishtar/IntegerConvertible.h>
#include <Ishtar/Serialization.h>


namespace Ishtar
{

///////////////////////////////////////////////////////////////////////////////
//
// I/O Data
//

template< typename Derived, typename ValueType >
class IoData;


template< typename Derived >
class IoData< Derived, Bool >
{
};


template< typename Derived >
class IoData< Derived, Int32 > : public IntConvertible< IoData< Derived, Int32 > >
{
public:
    Int ToInt() const { return this->Io_GetInt32(); }

    template< typename StreamWriter >
    void WriteToStream( StreamWriter& sw ) const
    {
        sw.WriteInt32( this->Io_GetInt32() );
    }

    template< typename StreamReader >
    void ReadFromStream( StreamReader& sr )
    {
         this->Io_SetInt32( sr.ReadInt32() );
    }
    
private:
    Int32 Io_GetInt32() const { return static_cast< const Derived& >( *this ).Value(); }

    void Io_SetInt32( Int32 value )
    {
        static_cast< Derived& >( *this ) = Derived( value );
    }
};


template< typename Derived >
class IoData< Derived, Int16 > : public IntConvertible< IoData< Derived, Int16 > >
{
public:
    Int ToInt() const { return this->Io_GetInt16(); }

    template< typename StreamWriter >
    void WriteToStream( StreamWriter& sw ) const
    {
        sw.WriteInt16( this->Io_GetInt16() );
    }

    template< typename StreamReader >
    void ReadFromStream( StreamReader& sr )
    {
        this->Io_SetInt16( sr.ReadInt16() );
    }

private:
    Int16 Io_GetInt16() const { return static_cast< const Derived& >( *this ).Value(); }

    void Io_SetInt16( Int16 value )
    {
        static_cast< Derived& >( *this ) = Derived( value );
    }
};


///////////////////////////////////////////////////////////////////////////////

} // namespace Ishtar

#endif // __ISHTAR_IO_IO_DATA_H
